package com.hibase.core.rule.mapper.custom;


import com.hibase.common.exception.HibaseAssert;
import org.apache.commons.lang.text.StrSubstitutor;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * Mapper 自定义 接口
 * </p>
 *
 * @author hufeng
 * @since 2019-04-04
 */
public interface CustomMapper {

    /**
     * sql命名
     */
    String PARAM_SQL_TEXT = "sql";

    @SelectProvider(type = CustomSelectProvider.class, method = "select")
    List<Map<String, Object>> select(Map<String, Object> map);

    class CustomSelectProvider {

        public String select(Map<String, Object> map) {

            /**
             * 传入结构
             * sql语句："sql":"sql"
             * 参数值：key:value
             */
            HibaseAssert.notEmpty(map, "参数不合法，传入map为空");
            HibaseAssert.isTrue(map.containsKey(PARAM_SQL_TEXT), "参数不合法，传入sql参数为空");
            String sql = map.get(PARAM_SQL_TEXT).toString();

            return StrSubstitutor.replace(sql, map);
        }
    }
}
